/**********************************************************************************************
Do-file:		PretrendsData.do
Date:			4/5/24
Author:			Stephanie Karol
Description:	This file produces the data used in pretrends graphs for Figure 2.

***********************************************************************************************/


reghdfe ihscontribcur anyafter after if (fisyr == 2018 | fisyr == 2019), a(ein) vce(cluster fips) 
gen insampein = e(sample)

drop if insampein != 1
keep ein
duplicates drop

save "insampein.dta", replace

import delimited using "core.csv", clear

merge m:1 ein using "insampein.dta"
keep if _merge == 3

drop v1
duplicates drop
gen urlnum = substr(url,43,14)
destring urlnum, float replace
egen maxurlnum = max(urlnum), by(ein fisyr)
keep if maxurlnum == urlnum
drop urlnum maxurlnum url
duplicates drop

xtset ein fisyr

foreach var of varlist contribcur ggr salcur pse {
replace `var' = "." if `var' == "NA"
destring `var', replace	
}

replace ggr = 0 if ggr == .

foreach var of varlist contribcur ggr salcur pse totemp {
	gen ihs`var' = ln(`var' + sqrt(`var'^2 +1))
}

keep if fisyr >= 2014 & fisyr <= 2018

egen numobs = count(fisyr), by(ein)

gen fips = .
replace fips = 1 if state == "AL"
replace fips = 2 if state == "AK"
replace fips = 4 if state == "AZ"
replace fips = 5 if state == "AR"
replace fips = 6 if state == "CA"
replace fips = 8 if state == "CO"
replace fips = 9 if state == "CT"
replace fips = 10 if state == "DE"
replace fips = 11 if state == "DC"
replace fips = 12 if state == "FL"
replace fips = 13 if state == "GA"
replace fips = 15 if state == "HI"
replace fips = 16 if state == "ID"
replace fips = 17 if state == "IL"
replace fips = 18 if state == "IN"
replace fips = 19 if state == "IA"
replace fips = 20 if state == "KS"
replace fips = 21 if state == "KY"
replace fips = 22 if state == "LA"
replace fips = 23 if state == "ME"
replace fips = 24 if state == "MD"
replace fips = 25 if state == "MA"
replace fips = 26 if state == "MI"
replace fips = 27 if state == "MN"
replace fips = 28 if state == "MS"
replace fips = 29 if state == "MO"
replace fips = 30 if state == "MT"
replace fips = 31 if state == "NE"
replace fips = 32 if state == "NV"
replace fips = 33 if state == "NH"
replace fips = 34 if state == "NJ"
replace fips = 35 if state == "NM"
replace fips = 36 if state == "NY"
replace fips = 37 if state == "NC"
replace fips = 38 if state == "ND"
replace fips = 39 if state == "OH"
replace fips = 40 if state == "OK"
replace fips = 41 if state == "OR"
replace fips = 42 if state == "PA"
replace fips = 44 if state == "RI"
replace fips = 45 if state == "SC"
replace fips = 46 if state == "SD"
replace fips = 47 if state == "TN"
replace fips = 48 if state == "TX"
replace fips = 49 if state == "UT"
replace fips = 50 if state == "VT"
replace fips = 51 if state == "VA"
replace fips = 53 if state == "WA"
replace fips = 54 if state == "WV"
replace fips = 55 if state == "WI"
replace fips = 56 if state == "WY"
replace fips = 60 if state == "AS"
replace fips = 66 if state == "GU"
replace fips = 69 if state == "MP"
replace fips = 72 if state == "PR"
replace fips = 78 if state == "VI"

foreach var of varlist contribcur ggr salcur pse totemp {
	qui reghdfe ihs`var' i.anyexposure#i.fisyr i.fisyr if numobs == 5, a(ein) vce(cluster fips) nocons
	est sto estihs`var'
}

mat define pretrendmat = J(30,2,.) 

 est restore estihscontribcur
 mat pretrendmat[1,1]=e(b)[1,6]
 mat pretrendmat[2,1]=e(b)[1,7]
 mat pretrendmat[3,1]=e(b)[1,8]
 mat pretrendmat[4,1]=e(b)[1,9]
 mat pretrendmat[5,1]=e(b)[1,10]
 
 mat pretrendmat[1,2]=sqrt(e(V)[6,6])
 mat pretrendmat[2,2]=sqrt(e(V)[7,7])
 mat pretrendmat[3,2]=sqrt(e(V)[8,8])
 mat pretrendmat[4,2]=sqrt(e(V)[9,9])
 mat pretrendmat[5,2]=sqrt(e(V)[10,10])
 
 testparm 1.anyexposure#2014b.fisyr 1.anyexposure#2015.fisyr 1.anyexposure#2016.fisyr 1.anyexposure#2017.fisyr 1.anyexposure#2018.fisyr, equal
 mat pretrendmat[6,1]=r(F)
 mat pretrendmat[6,2]=r(p)
 
  est restore estihsggr
 mat pretrendmat[7,1]=e(b)[1,6]
 mat pretrendmat[8,1]=e(b)[1,7]
 mat pretrendmat[9,1]=e(b)[1,8]
 mat pretrendmat[10,1]=e(b)[1,9]
 mat pretrendmat[11,1]=e(b)[1,10]
 
 mat pretrendmat[7,2]=sqrt(e(V)[6,6])
 mat pretrendmat[8,2]=sqrt(e(V)[7,7])
 mat pretrendmat[9,2]=sqrt(e(V)[8,8])
 mat pretrendmat[10,2]=sqrt(e(V)[9,9])
 mat pretrendmat[11,2]=sqrt(e(V)[10,10])
 
 testparm 1.anyexposure#2014b.fisyr 1.anyexposure#2015.fisyr 1.anyexposure#2016.fisyr 1.anyexposure#2017.fisyr 1.anyexposure#2018.fisyr, equal
 mat pretrendmat[12,1]=r(F)
 mat pretrendmat[12,2]=r(p)
 
  est restore estihssalcur
 mat pretrendmat[13,1]=e(b)[1,6]
 mat pretrendmat[14,1]=e(b)[1,7]
 mat pretrendmat[15,1]=e(b)[1,8]
 mat pretrendmat[16,1]=e(b)[1,9]
 mat pretrendmat[17,1]=e(b)[1,10]
 
 mat pretrendmat[13,2]=sqrt(e(V)[6,6])
 mat pretrendmat[14,2]=sqrt(e(V)[7,7])
 mat pretrendmat[15,2]=sqrt(e(V)[8,8])
 mat pretrendmat[16,2]=sqrt(e(V)[9,9])
 mat pretrendmat[17,2]=sqrt(e(V)[10,10])
 
 testparm 1.anyexposure#2014b.fisyr 1.anyexposure#2015.fisyr 1.anyexposure#2016.fisyr 1.anyexposure#2017.fisyr 1.anyexposure#2018.fisyr, equal
 mat pretrendmat[18,1]=r(F)
 mat pretrendmat[18,2]=r(p)
 
  est restore estihstotemp
 mat pretrendmat[19,1]=e(b)[1,6]
 mat pretrendmat[20,1]=e(b)[1,7]
 mat pretrendmat[21,1]=e(b)[1,8]
 mat pretrendmat[22,1]=e(b)[1,9]
 mat pretrendmat[23,1]=e(b)[1,10]
 
 mat pretrendmat[19,2]=sqrt(e(V)[6,6])
 mat pretrendmat[20,2]=sqrt(e(V)[7,7])
 mat pretrendmat[21,2]=sqrt(e(V)[8,8])
 mat pretrendmat[22,2]=sqrt(e(V)[9,9])
 mat pretrendmat[23,2]=sqrt(e(V)[10,10])
 
 testparm 1.anyexposure#2014b.fisyr 1.anyexposure#2015.fisyr 1.anyexposure#2016.fisyr 1.anyexposure#2017.fisyr 1.anyexposure#2018.fisyr, equal
 mat pretrendmat[24,1]=r(F)
 mat pretrendmat[24,2]=r(p)
 
  est restore estihspse
 mat pretrendmat[25,1]=e(b)[1,6]
 mat pretrendmat[26,1]=e(b)[1,7]
 mat pretrendmat[27,1]=e(b)[1,8]
 mat pretrendmat[28,1]=e(b)[1,9]
 mat pretrendmat[29,1]=e(b)[1,10]
 
 mat pretrendmat[25,2]=sqrt(e(V)[6,6])
 mat pretrendmat[26,2]=sqrt(e(V)[7,7])
 mat pretrendmat[27,2]=sqrt(e(V)[8,8])
 mat pretrendmat[28,2]=sqrt(e(V)[9,9])
 mat pretrendmat[29,2]=sqrt(e(V)[10,10])
 
 testparm 1.anyexposure#2014b.fisyr 1.anyexposure#2015.fisyr 1.anyexposure#2016.fisyr 1.anyexposure#2017.fisyr 1.anyexposure#2018.fisyr, equal
 mat pretrendmat[30,1]=r(F)
 mat pretrendmat[30,2]=r(p)
 
 preserve
svmat pretrendmat
rename pretrendmat1 pretrendmatb
rename pretrendmat2 pretrendmatse
keep pretrendmat*
drop if _n > 30
export delimited using "pretrendmat.csv", replace
restore